package com.falcon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.falcon.core.utils.BeanUtils;
import com.falcon.domain.dos.QuestionDO;
import com.falcon.domain.po.QuestionPO;
import com.falcon.domain.req.QuestionQuery;
import com.falcon.domain.req.QuestionRequest;
import com.falcon.mapper.IQuestionMapper;
import com.falcon.mapper.IQuestionOptionMapper;
import com.falcon.service.IQuestionOptionService;
import com.falcon.service.IQuestionService;
import com.falcon.mvc.domain.BasePager;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swan.mybatis.condition.Condition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


@Slf4j
@Service
public class QuestionService implements IQuestionService {

    @Autowired
    private IQuestionMapper questionMapper;

    @Autowired
    private IQuestionOptionMapper questionOptionMapper;

    @Autowired
    private IQuestionOptionService questionOptionService;

    @Override
    public Long save(QuestionRequest request){
        QuestionPO questionPO = BeanUtil.copyProperties(request, QuestionPO.class);

        // 保存主表
        this.questionMapper.insertNotNull(questionPO);

        // 保存子表
        this.questionOptionService.batchSave(questionPO.getId(), request.getOptions());

        return questionPO.getId();
    }

    @Override
    public Boolean update(Long id, QuestionRequest request){

        // 更新主表
        QuestionPO questionPO = BeanUtil.copyProperties(request, QuestionPO.class);
        questionPO.setId(id);
        boolean success = this.questionMapper.updateNotNull(questionPO);
        if (!success) {
            return false;
        }

        // 删除子表
        this.questionOptionService.deleteByQuestionIds(Arrays.asList(questionPO.getId()));

        // 重新创建子表
        this.questionOptionService.batchSave(questionPO.getId(), request.getOptions());

        return true;
    }

    @Override
    public Boolean deleteById(Long id) {
        // 删除主表
        boolean success = this.questionMapper.deleteById(id);

        // 删除子表
        this.questionOptionService.deleteByQuestionIds(Arrays.asList(id));

        return success;
    }

    @Override
    public QuestionDO queryById(Long id){

        QuestionPO questionPO = this.questionMapper.selectById(id);

        QuestionDO questionDO = BeanUtil.copyProperties(questionPO, QuestionDO.class);

        return questionDO;
    }


    @Override
    public List<QuestionDO> queryList(QuestionQuery request){

        Condition condition = request.toCondition();

        List<QuestionPO> poList = this.questionMapper.selectList(condition);

        List<QuestionDO> doList = BeanUtil.copyToList(poList, QuestionDO.class);

        return doList;
    }

    @Override
    public BasePager<QuestionDO> queryPager(QuestionQuery request){


        PageInfo<QuestionPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.questionMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, QuestionDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {
        // 删除主表
        int delNum = this.questionMapper.deleteByIds(ids);

        // 删除
        this.questionOptionService.deleteByQuestionIds(ids);

        return delNum;
    }




}

